#include "s3e.h"
#include "Iw2D.h"
#include <list>
#include <vector>
#include "ui_menu.h"
#include "ui_page.h"
#include "ui_widgets.h"

namespace UI
{

Page::Page( const char* a_Name )
	: m_Name(a_Name)
{
}

Page::~Page()
{
 	for (std::list<UI::Widget*>::iterator widget_it = m_Widgets.begin(); widget_it != m_Widgets.end(); ++widget_it)
 	{
 		(*widget_it)->Delete();
 	}

	for (std::list<UI::Widget*>::iterator widget_it = m_Widgets.begin(); widget_it != m_Widgets.end(); widget_it = m_Widgets.begin())
	{
		UI::Widget* widget = (*widget_it);
		m_Widgets.pop_front();
		delete widget;
	}
}

void Page::Update( float a_DT )
{
	for (std::list<UI::Widget*>::iterator widget_it = m_Widgets.begin(); widget_it != m_Widgets.end(); ++widget_it)
	{
		(*widget_it)->Update(a_DT);
	}
}

void Page::Render()
{
	for (std::list<UI::Widget*>::iterator widget_it = m_Widgets.begin(); widget_it != m_Widgets.end(); ++widget_it)
	{
		(*widget_it)->Render();
	}
}

void Page::Delete()
{
	for (std::list<UI::Widget*>::iterator widget_it = m_Widgets.begin(); widget_it != m_Widgets.end(); ++widget_it)
	{
		(*widget_it)->Delete();
	}
	
	for (std::list<UI::Widget*>::iterator widget_it = m_Widgets.begin(); widget_it != m_Widgets.end(); widget_it = m_Widgets.begin())
	{
		UI::Widget* widget = (*widget_it);
		m_Widgets.pop_front();
		delete widget;
	}
}

const char* Page::GetName()
{
	return m_Name;
}

void Page::AddWidget( UI::Widget* a_Widget )
{
	a_Widget->m_ParentMenu = m_ParentMenu;
	m_Widgets.push_back(a_Widget);
}

void Page::SetParentMenu( UI::Menu* a_Menu )
{
	m_ParentMenu = a_Menu;
	
	for (std::list<UI::Widget*>::iterator widget_it = m_Widgets.begin(); widget_it != m_Widgets.end(); ++widget_it)
	{
		(*widget_it)->SetParentMenu(m_ParentMenu);
	}
}

}